package br.com.dev.tcc.view;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.ProgressBar;
import br.com.dev.tcc.R;
import br.com.dev.tcc.database.DatabaseHelper;

public class SplashActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.splash);
		final ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar);
		
		new AsyncTask<Void, Integer, Void>(){

			@Override
			protected Void doInBackground(Void... params) {

				try {
					Thread.sleep(800);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				publishProgress(15);
				
				DatabaseHelper helper = new DatabaseHelper(SplashActivity.this);
				
				try {
					Thread.sleep(1300);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				publishProgress(70);
				
				try {
					Thread.sleep(600);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				helper.close();
				
				return null;
			}
			
			@Override
			protected void onProgressUpdate(Integer[] values) {
				
				int progress = values[0];
				
				bar.setProgress(progress);
			};
			
			@Override
			protected void onPostExecute(Void result) {
				
				bar.setProgress(100);
			};
			
		}.execute();
		
		Animation anim = new ScaleAnimation(0.8f, 1f, 0.8f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		anim.setDuration(3300);
		anim.setInterpolator(new AccelerateInterpolator());
		anim.setFillAfter(true);
		anim.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation a) {}
			
			@Override
			public void onAnimationRepeat(Animation a) {}
			
			@Override
			public void onAnimationEnd(Animation a) {

				/*startActivity(new Intent(SplashActivity.this, HomeActivity.class));
				finish();*/
			}
		});
		
		startActivity(new Intent(SplashActivity.this, HomeActivity.class));
		finish();
		//findViewById(R.id.img).startAnimation(anim);
	}
	
	@Override
	public void onBackPressed() {

		return;
	}
}
